using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.Views;
using Android.Widget;

namespace jivemessenger
{
    class ContactArrayAdapter : ArrayAdapter<Contact>
    {
        private TextView ContactNameView;
        public List<Contact> Contacts = new List<Contact>();
        private LinearLayout wrapper;
        public ContactArrayAdapter(Context context, int textViewResourceId)
            : base(context, textViewResourceId)
        {
        }

        public void add(Contact obj)
        {
            this.Contacts.Add(obj);
            //super.add(object);
            base.Add(obj);
            
            NotifyDataSetChanged();
        }

        public void remove(Contact obj)
        {
            this.Contacts.Remove(obj);
            base.Remove(obj);
            NotifyDataSetChanged();
        }

        public int GetCount()
        {
            return this.Contacts.Count();
      
        }

        //public IEnumerable<Contact> GetContacts()
        //{
        //    return Contacts;
        //}

        public Contact getItem(int index)
        {
            return this.Contacts[index];
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;
            if (row == null)
            {
                LayoutInflater inflater = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
                row = inflater.Inflate(Resource.Layout.ContactListItem, parent, false);
            }

            wrapper = (LinearLayout)row.FindViewById(Resource.Id.contactwrapper);

            Contact contact = getItem(position);

            this.ContactNameView = (TextView)row.FindViewById(Resource.Id.contactname);

            this.ContactNameView.SetText(contact.User.PrettyName, TextView.BufferType.Normal);

            //this.ContactNameView.SetBackgroundResource(contact.Left ? Resource.Drawable.bubble_yellow : Resource.Drawable.bubble_green);
            wrapper.SetGravity(GravityFlags.Left);

            return row;
        }

        //public Bitmap decodeToBitmap(byte[] decodedByte)
        //{
        //    return BitmapFactory.DecodeByteArray(decodedByte, 0, decodedByte.Length);
        //}
    }
}